using UnityEngine;

namespace Game.Debuger
{
	public class FPSDisplayer : MonoBehaviour
	{
		public float updateInterval = 0.5f;

		private double lastTime;

		private int frames;

		private float fps;

		private void Start()
		{
			lastTime = Time.realtimeSinceStartup;
			frames = 0;
		}

		private void OnGUI()
		{
			GUI.backgroundColor = Color.black;
			GUI.Button(new Rect(-2f, -2f, 100f, 30f), "FPS：" + fps.ToString("f2"));
		}

		private void Update()
		{
			frames++;
			float realtimeSinceStartup = Time.realtimeSinceStartup;
			if ((double)realtimeSinceStartup > lastTime + (double)updateInterval)
			{
				fps = (float)((double)frames / ((double)realtimeSinceStartup - lastTime));
				frames = 0;
				lastTime = realtimeSinceStartup;
			}
		}
	}
}
